use crate::utils::time_cost;
#[allow(non_snake_case)]
#[macro_export]
macro_rules! str_join {
    ($($str:expr),+)=>{
    {
         let mut rs = std::string::String::new();
         $(rs.push_str($str);)+
         rs
        }
    };
}

pub fn find_char_boundary(s: &str, index: usize) -> usize {
    if s.len() <= index {
        return index;
    }

    let mut new_index = index;
    while !s.is_char_boundary(new_index) {
        new_index += 1;
    }

    new_index
}

// pub fn str_between_join(text: &str, rexVec: Vec<&str>, join: &str) -> Option<String> {
//     if !(text.len() == 0 || rexVec.len() < 2) {
//         let mut idx = index_of(text, rexVec[0], 0)?;
//         let mut rs = String::new();
//         for x in 1..rexVec.len() {
//             let i = index_of(text, rexVec[x], idx as usize + rexVec[x - 1].len())?;
//             let f1 = idx as usize + rexVec[x - 1].len();
//             rs.push_str(sub_str(text, f1, i as usize - f1).unwrap_or(""));
//             rs.push_str(join);
//             idx = i;
//         }
//         return Some(rs);
//     }
//     None
// }

pub fn str_between_join(text: &str, rex_vec: &[&str], join: &str) -> Option<String> {
    if !(text.len() == 0 || rex_vec.len() < 2) {
        let mut rs = String::new();
        let mut start = text.find(rex_vec[0])?;
        let mut next: usize = 0;
        let mut next_text = &text[(start + rex_vec[0].len())..];
        for x in 1..rex_vec.len() {
            next = next_text.find(rex_vec[x])?;
            rs.push_str(&next_text[..next]);
            rs.push_str(join);
            next_text = &next_text[(next + rex_vec[x].len())..];
        }
        return Some(rs);
    }
    None
}

// 查询是否存在子串并且返回下标
pub fn index_of(text: &str, pat: &str, start: usize) -> Option<usize> {
    if !(text.len() == 0 || pat.len() == 0 || pat.len() > text.len() || start > text.len()) {
        (&text[start..]).find(pat)
    } else {
        None
    }
}
// 查询是否有任意字符串匹配
pub fn index_of_any(text: &str, pats: &[&str], start: usize) -> bool {
    if !(text.len() == 0 || pats.len() == 0 || start > text.len()) {
        for x in pats {
            if let Some(_) = index_of(text, *x, 0) {
                return true;
            };
        }
    }
    false
}
// 获取子字符串,已进行边界处理过
pub fn sub_str(text: &str, a: usize, b: usize) -> Option<&str> {
    if a + b >= text.len() || text.len() == 0 {
        None
    } else {
        Some(&text[find_char_boundary(text, a)..find_char_boundary(text, a + b)])
    }
}

#[test]
pub fn shit() {
    let split = "tatr;asta;".split(";").collect::<Vec<_>>();
    let x = (split[0].clone(), split[1].clone());
    println!("{:?}", x);

}
